﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyWebPagesStarterKit.Controls;
using MyWebPagesStarterKit;
using System.Data;
using MyWebPagesStarterKit.Rewriter;
using System.Collections;

public partial class SectionControls_RBlogSidebar : SectionControlBaseClass
{
    private RBlogSidebar _section;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (_section.ShowCalendar)
        {
            calendarBlog.Visible = true;
            litblog.Text = "<div id=\"blogCalendar\" style=\"display:''\">";
        }
        else
        {
            litblog.Text = "<div id=\"blogCalendar\" style=\"display:none\">";
        }
    }

    protected void Page_PreRender(object sender, EventArgs e)
    {
        updateViews();
    }

    private void updateViews()
    {
        if (ViewMode == ViewMode.Edit)
        {
            multiview.SetActiveView(editView);
            populateList(SiteMap.RootNode, 0);

        }
        else
        {
            multiview.SetActiveView(readonlyView);
            if (_section.PageID != null)
            {
                    LoadTags();
            }
        }
    }

    string o = string.Empty;
    private void LoadTags()
    {
        SortedList list = new SortedList();
        foreach (DataRow row in _section.GetItems(_section.PageID).Table.Rows)
        {
            List<RBlog.Tags> tags = ((List<RBlog.Tags>)row["Tags"]);
            foreach (RBlog.Tags tag in tags)
            {
                if(!list.ContainsKey(tag.Name))
                    list.Add(tag.Name,1);
                else
                    list[tag.Name] = ((Convert.ToInt32(list[tag.Name]))+1);
            }   
        }

        List(list);
    }


    public void List(SortedList list)
    {
        bloglist.Text = string.Empty;
        string url = new WebPage(_section.PageID).VirtualPath.Replace("~/", "");
        foreach (DictionaryEntry item in list)
        {
            int c = Convert.ToInt32(item.Value);
            if (c > 10)
                c = 10;
            
            bloglist.Text += string.Format("<a href=\"{0}?tag={1}\" class=\"tag{3}\">{2}</a> ", url, item.Key, item.Key, c);
        }

        //calendar selection
        foreach (DataRow row in _section.GetItems(_section.PageID).Table.Rows)
        {
            calendarBlog.SelectedDates.Add(Convert.ToDateTime(row["Created"]));
        }
    }


    /// <summary>
    /// Adds date and url
    /// </summary>
    /// <param name="link">The link to perform rewrite prefix to.</param>
    /// <returns>A browserfriendly url</returns>
    private string GetPrefix(string link)
    {
        return string.Format("{0}{1}", SectionRewriter.GetPrefix("RBlog"), link).ToLower();
    }

    protected string GetUrl(string link)
    {
        return string.Format("{0}{1}", SectionRewriter.GetPrefix("RBlog"), link);
    }

   
    private void populateList(SiteMapNode parentNode, int level)
    {
        WebSite _website = WebSite.GetInstance();

        foreach (SiteMapNode node in parentNode.ChildNodes)
        {
            if (node["pageId"] != null)
            {
                string title = node.Title;
                for (int i = 0; i < level; i++)
                    title = "..." + title;

                if (node["visible"] == false.ToString())
                    title += " (Hidden)";

                if (node["hidden"] == true.ToString())
                    title += " (Hidden)";

                if (node["pageId"] == _website.HomepageId)
                    title += " (HomePage)";

                ListItem item = new ListItem(title, node["pageId"]);

                lstPages.Items.Add(item);
                populateList(node, level + 1);

                if (Convert.ToString(node["pageId"]) == _section.PageID)
                    lstPages.SelectedValue = _section.PageID;
            }
        }
    }

    protected void Save(object sender, EventArgs e)
    {
        _section.PageID = lstPages.SelectedValue;
        _section.ShowCalendar = chkViewCal.Checked;
        _section.SaveData();
    }

    protected void calendarBlog_DayRender(object sender, DayRenderEventArgs e)
    {
        if (!e.Day.IsSelected)
        {
            e.Cell.Controls.Clear();
            e.Cell.Text = e.Day.DayNumberText;
        }
    }


    protected void calendarBlog_SelectionChanged(object sender, EventArgs e)
    {
 
        Response.Redirect(string.Format("{0}?date={1}", new WebPage(_section.PageID).VirtualPath.Replace("~", ""), calendarBlog.SelectedDate.ToShortDateString()));
    }


    public override bool HasAdminView
    {
        get { return true; }
    }

    public override string InfoUrl
    {
        get { return ""; }
    }

    public override ISection Section
    {
        get
        {
            return (RBlogSidebar)_section;
        }
        set
        {
            _section = (RBlogSidebar)value;
        }
    }
}
